package com.ias.agdis_r.provider;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.util.Log;

import java.sql.SQLException;

/**
 * Created by qibo.yu on 2015/9/22.
 */
public class AccountStateProvider extends ContentProvider {

    public static final String PROVIDER_URI = "content://com.ias.agdis_r.provider.AccountStateProvider";

    private SQLiteDatabase sqlDB;
    private DBHelper dbHelper;


    public static final String COLUMN_USER_NAME = "_id";
    public static final String COLUMN_NAME = "_name";
    public static final String COLUMN_PASSWORLD = "_password";
    public static final String COLUMN_IS_LOGON = "_is_logon";

    private static final String DATABASE_NAME = "C_AccountStateProvider.db";
    private static final int DATABASE_VERSION = 1;
    private static final String TABLE_NAME = "user_state";


    private static final String TAG = "C_log";


    private static class DBHelper extends SQLiteOpenHelper {


        public DBHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }


        @Override
        public void onCreate(SQLiteDatabase db) {
            StringBuilder sqlBuilder = new StringBuilder();
            sqlBuilder.append("CREATE TABLE ").append(TABLE_NAME).append("(");

            sqlBuilder.append(COLUMN_USER_NAME).append(" ").append("VARCHAR(30) PRIMARY KEY,");
            sqlBuilder.append(COLUMN_NAME).append(" ").append("VARCHAR(30),");
            sqlBuilder.append(COLUMN_PASSWORLD).append(" ").append("VARCHAR(30),");
            sqlBuilder.append(COLUMN_IS_LOGON).append(" ").append("BOOLEAN DEFAULT true");
            sqlBuilder.append(")");

            Log.i(TAG, "creae table with sql: " + sqlBuilder.toString());
            db.execSQL(sqlBuilder.toString());
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
            onCreate(db);
        }
    }


    @Override
    public boolean onCreate() {
        Log.i(TAG, "provider create");
        this.dbHelper = new DBHelper(getContext());
        return this.dbHelper != null;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        Log.d(TAG, "provider query execute");
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        qb.setTables(TABLE_NAME);
        Cursor cursor = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder);
        cursor.setNotificationUri(getContext().getContentResolver(), uri);
        return cursor;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        sqlDB = dbHelper.getWritableDatabase();
        long rowId = sqlDB.insert(TABLE_NAME, "", values);
        sqlDB.close();
        return null;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        int count = db.delete(TABLE_NAME, selection, selectionArgs);
        db.close();
        return count;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();

        int count = db.update(TABLE_NAME, values, selection, selectionArgs);
        db.close();
        return count;
    }
}
